package ewa

import (
	"net/http"
  "fmt"
	"github.com/labstack/echo"
)

//type NonRestrictRouteCb func(echo.Context) error

/*  Non-Restrict Route POST, 
   不需要cookies, 
   不需要token验证  */
func CbNonCookieTokenPost(c echo.Context) error {
  //fmt.Printf("Path: %s\n", c.Path())	//such like as '/index.html'
  utype, uri := HttpReqPathSuffix(c.Path())

  if utype == URITYPE_RES {

  }else if utype == URITYPE_API {
    if uri == CfgRoute.ApiLogin {
      // 特殊API: 用户登录 不需要上传Cookie或者Token
      // 客户端上传用户名+密码, Echo生成JWT Token
      // 然后返回JWT Token给客户端
      if UserAuthSuccess(c) == true {
        return c.JSON(http.StatusOK, echo.Map{
          "token": JwtTokenCreate(c.FormValue("Username")),
        })
      }
    }else if uri == "/passok" {  // 用户密码验证API, 返回 yes , no
      if UserAuthSuccess(c) == true {
        return c.String(http.StatusOK, "yes")
      }else{
        return c.String(http.StatusOK, "no")
      }
    }else{
      //
      if CfgRoute.MApiAttr[uri].SyncAck == true {
        return SyncApiResponse(uri, c)
      }else{
        return AsyncApiResponse(uri, c)
      }
    }
  }
  return nil
}

func RouteRegistry_NonCookieTokenPost(route string, e *echo.Echo, cb NonRestrictRouteCb) {
  l:=fmt.Sprintf("----Echo/> regist NonCookieTokenPost '%s'", route);Log(l)
	e.POST(route, (echo.HandlerFunc)(cb))
}